package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.db.Questionnaire;
import com.tanhua.domain.mongo.Questions;
import com.tanhua.domain.mongo.Report;
import com.tanhua.domain.mongo.ReportUser;
import com.tanhua.domain.vo.QuestionnaireVo;
import com.tanhua.dubbo.api.LockServiceApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author HoshizoraL
 * @Date 2021/4/20 11:17
 */
@Service
public class SoulTestApiImpl implements SoulTestApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference
    private LockServiceApi lockServiceApi;

    /**
     * 存入用户和报告表
     * @param ru
     */
    @Override
    public void saveReportUser(ReportUser ru) {
        ReportUser one = mongoTemplate.findOne(new Query(Criteria.where("reportId").is(ru.getReportId())
                .and("userId").is(ru.getUserId())), ReportUser.class);
        if (one == null) {
            mongoTemplate.save(ru);
        } else {
            Update update = new Update();
            update.set("createdTime",ru.getCreatedTime());
            mongoTemplate.updateFirst(new Query(Criteria.where("reportId").is(ru.getReportId())
                    .and("userId").is(ru.getUserId())),update,ReportUser.class);
        }
    }

    /**
     * 获取报告id相同的userId集合，根据创建时间排序获取i个用户
     * @param id
     * @param i
     * @return
     */
    @Override
    public List<ReportUser> findUsers(String id, int i) {
        Query query = new Query(Criteria.where("reportId").is(new ObjectId(id)));
        Pageable pageable = PageRequest.of(0, i, Sort.by(Sort.Order.desc("CreatedTime")));
        query.with(pageable);
        List<ReportUser> list = mongoTemplate.find(query, ReportUser.class);
        return list;
    }

    /**
     * 根据报告Id查询报告
     * @param id
     * @return
     */
    @Override
    public Report getReportById(String id) {
        Report report = mongoTemplate.findById(new ObjectId(id),Report.class);
        return report;
    }

    /**
     * 根据Id获取试卷id
     * @param questionId
     * @return
     */
    @Override
    public Long getTest(String questionId) {
        return mongoTemplate.findById(questionId, Questions.class).getTestId();
    }

    /**
     * 根据分数和测试Id获取报告Id
     * @param score
     * @param testId
     * @return
     */
    @Override
    public Report getReportId(int score, Long testId) {
        //0~40 评价一:1  41~80 评价二:2 81~120 评价三:3
        int level = score > 40 ? score > 80 ? 3 : 2 : 1;
        Query query = new Query(Criteria.where("level").is(level).and("testId").is(testId));
        Report report = mongoTemplate.findOne(query, Report.class);
        return report;
    }

    @Override
    public void updateLock(Long userId, Long testId) {
        //lockServiceApi.updateLock(userId,testId);
        if (testId==1){
            lockServiceApi.update(4l,userId);
        }
        if (testId==2){
            lockServiceApi.update(5l,userId);
        }
    }

    @Override
    public int getLock(Long userId) {
        return lockServiceApi.getLock(userId);
    }

    @Override
    public List<QuestionnaireVo> findTests() {
        return  mongoTemplate.findAll(QuestionnaireVo.class);
    }

}
